package com.easyjava.qingke.memo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.easyjava.qingke.memo.mybatis.IDao.LabelMapper;
import com.easyjava.qingke.memo.mybatis.IDao.MemoContentMapper;
import com.easyjava.qingke.memo.mybatis.IDao.MemoLabelMapper;
import com.easyjava.qingke.memo.mybatis.pojo.Days;
import com.easyjava.qingke.memo.mybatis.pojo.Label;
import com.easyjava.qingke.memo.mybatis.pojo.MemoContent;
import com.easyjava.qingke.memo.mybatis.pojo.MemoLabel;

public class MemoDao {
	
	public MemoDao(){}
	String resource = "mybatis_conf.xml";
	InputStream is = MemoDao.class.getClassLoader().getResourceAsStream(resource);
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	SqlSession session = sessionFactory.openSession();
	
	public void addMemoContent(MemoContent memoContent){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		mcm.insert(memoContent);
		session.commit();
	}
	public void deleteMemoContent(int id){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		mcm.deleteByPrimaryKey(id);
		session.commit();
	}
	public void ChangeMemoContent(MemoContent memoContent){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		mcm.updateByPrimaryKeySelective(memoContent);
		session.commit();
	}
	public void addLabel(Label label){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		lm.insert(label);
		session.commit();
	}
	public void deleteLabel(int id){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		lm.deleteByPrimaryKey(id);
		session.commit();
	}
	public void ChangeLabel(Label label){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		lm.updateByPrimaryKeySelective(label);
		session.commit();
	}
	public void addMemoLabel(MemoLabel memoLabel){
		MemoLabelMapper mlm =session.getMapper(MemoLabelMapper.class);
		mlm.insert(memoLabel);
		session.commit();
	}
	
	public void deleteMemoLabel(int id){
		MemoLabelMapper mlm =session.getMapper(MemoLabelMapper.class);
		mlm.deleteByPrimaryKey(id);
		session.commit();
	}
	public void changeMemoLabel(MemoLabel memoLabel){
		MemoLabelMapper mlm =session.getMapper(MemoLabelMapper.class);
		mlm.updateByPrimaryKeySelective(memoLabel);
		session.commit();
	}
	
	public void selectMemoContent(){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		List<MemoContent> memoContent =mcm.getAllMemoContent();
		for(MemoContent mc:memoContent){
			System.out.println(mc);
		}
	}
	public void deleteMemoLabelByMemoContentId(int id ){
		MemoLabelMapper mlm=session.getMapper(MemoLabelMapper.class);
		List<MemoLabel> memoLabel=mlm.getByMemoContentId(id);
		for(MemoLabel ml:memoLabel){
			deleteMemoLabel(ml.getId());
		}
	}
	public void deleteMemoLabelByLabelId(int id ){
		MemoLabelMapper mlm=session.getMapper(MemoLabelMapper.class);
		List<MemoLabel> memoLabel=mlm.getByLabelId(id);
		for(MemoLabel ml:memoLabel){
			deleteMemoLabel(ml.getId());
		}
	}
	
	public void selectLabel(){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		List<Label> label =lm.getAllLabel();
		for(Label ll:label){
			System.out.println(ll);
		}
	}
	public void selectMemoLabel(){
		MemoLabelMapper mlm =session.getMapper(MemoLabelMapper.class);
		List<MemoLabel> memoLabel=mlm.getAllMemoLabel();
		for(MemoLabel ml:memoLabel){
			System.out.println(ml);
		}
	}
	public MemoContent getMemoContentById(int id){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		MemoContent mc=mcm.selectByPrimaryKey(id);
		return mc;
	}
	public Label getLabelById(int id){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		Label ll=lm.selectByPrimaryKey(id);
		return ll;
	}
	public MemoLabel getMemoLabelById(int id){
		MemoLabelMapper mlm=session.getMapper(MemoLabelMapper.class);
		MemoLabel ml=mlm.selectByPrimaryKey(id);
		return ml;
	}
	
	public void selectMemoContentByTime(Days days){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		List<MemoContent> memoContent=mcm.selectByCreateTime(days);
		Collections.sort(memoContent,new Comparator<MemoContent>(){

			public int compare(MemoContent o1, MemoContent o2) {
				String content1=o1.getContent();
				String content2=o2.getContent();
				Date time1=o1.getCreateTime();
				Date time2=o2.getCreateTime();
				if(time1.compareTo(time2)!=0){
					return -time1.compareTo(time2);
				}
				if(content1.equals(content2)){
					return content1.compareTo(content2);
				}
				return 0;
			}
		});
		for(MemoContent mc:memoContent){
			System.out.println(mc);
		}
	}
	
	public void selectMemoContentByLabelName(String name){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		MemoLabelMapper mlm=session.getMapper(MemoLabelMapper.class);
		Label label =lm.getByName(name);
		List<MemoLabel> memoLabel =mlm.getByLabelId(label.getId());
		List<MemoContent> memoContent = new ArrayList<MemoContent>();
		for(MemoLabel ml:memoLabel){
            MemoContent mc=mlm.selectMemoContentById(ml.getMemoContentId());
            memoContent.add(mc);
		}
		Collections.sort(memoContent,new Comparator<MemoContent>(){

			public int compare(MemoContent o1, MemoContent o2) {
				String content1=o1.getContent();
				String content2=o2.getContent();
				Date time1=o1.getCreateTime();
				Date time2=o2.getCreateTime();
				if(time1.compareTo(time2)!=0){
					return -time1.compareTo(time2);
				}
				if(content1.equals(content2)){
					return content1.compareTo(content2);
				}
				return 0;
			}
		});
		for(MemoContent mc:memoContent){
			System.out.println(mc);
		}
		
	}
	public void selectMemoContentByTitleOrContent(MemoContent memoContent){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		List<MemoContent> mc =mcm.getByTitleOrContent(memoContent);
		Collections.sort(mc,new Comparator<MemoContent>(){

			public int compare(MemoContent o1, MemoContent o2) {
				String content1=o1.getContent();
				String content2=o2.getContent();
				Date time1=o1.getCreateTime();
				Date time2=o2.getCreateTime();
				if(time1.compareTo(time2)!=0){
					return -time1.compareTo(time2);
				}
				if(content1.equals(content2)){
					return content1.compareTo(content2);
				}
				return 0;
			}
		});
		for(MemoContent mct:mc){
			System.out.println(mct);
		}
	}
	
	public boolean isMemoContentIdExists(int id){
		MemoContentMapper mcm =session.getMapper(MemoContentMapper.class);
		MemoContent mc =mcm.selectByPrimaryKey(id);
		if(mc!=null){
			return true;
		}
		return false;
	}
	public boolean isLabelIdExists(int id){
		LabelMapper lm=session.getMapper(LabelMapper.class);
		Label label =lm.selectByPrimaryKey(id);
		if(label!=null){
			return true;
		}
		return false;
	}
	public boolean isMemoLabelIdExists(int id){
		MemoLabelMapper mlm=session.getMapper(MemoLabelMapper.class);
		MemoLabel memoLabel =mlm.selectByPrimaryKey(id);
		if(memoLabel!=null){
			return true;
		}
		return false;
	}
	
	public boolean isLabelNameExists(String name){
		LabelMapper lm =session.getMapper(LabelMapper.class);
		Label label=lm.getByName(name);
		if(label!=null){
			return true;
		}
		return false;
	}
	public boolean isTitleExists(String title){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		MemoContent memoContent =mcm.getByTitle(title);
		if(memoContent!=null){
			return true;
		}
		return false;
	}
	public boolean isContentExists(String content){
		MemoContentMapper mcm=session.getMapper(MemoContentMapper.class);
		MemoContent memoContent =mcm.getByContent(content);
		if(memoContent!=null){
			return true;
		}
		return false;
	}
	public boolean isMemoContentExists(int id){
		MemoContentMapper mcm =session.getMapper(MemoContentMapper.class);
		MemoContent mc =mcm.selectByPrimaryKey(id);
		if(mc!=null){
			return true;
		}
		return false;
	}
}
